package tetris;

/**
 * ??????
 * @author Leslie Leung
 */
public class Tetromino {
	protected Cell[] cells;		//?????????cells???????
	protected Cell axis;	//?????
	protected Cell[] rotateCells;	//??????????????
	
	/**
	 * ?????????????????
	 * @param axis ???????cells???????0??Cell??????
	 * @param rotateCells ?????????????
	 */
	protected void anticlockwiseRotate(Cell axis, Cell[] rotateCells) {
		for (int i = 0; i < 3; i++) {
			int oldX = rotateCells[i].getX();
			int oldY = rotateCells[i].getY();
			int newX = axis.getX() - axis.getY() + oldY;
			int newY = axis.getY() + axis.getX() - oldX;
			rotateCells[i].setX(newX);
			rotateCells[i].setY(newY);
		}
	}
	
	/**
	 * ????????????????
	 * @param axis ???????cells???????0??Cell??????
	 * @param rotateCells ?????????????
	 */
	protected void clockwiseRotate(Cell axis, Cell[] rotateCells) {
		for (int i = 0; i < 3; i++) {
			int oldX = rotateCells[i].getX();
			int oldY = rotateCells[i].getY();
			int newX = axis.getX() - oldY + axis.getY();
			int newY = axis.getY() + oldX - axis.getX();
			rotateCells[i].setX(newX);
			rotateCells[i].setY(newY);
		}
	}
	
	/**
	 * ???????????????
	 */
	public  void moveDown(){
		for (int i = 0; i < cells.length;i++){
			int oldY = cells[i].getY();
			int newY = oldY + 1;
			cells[i].setY(newY);
		}
	}
	
	/**
	 * ??????????????
	 */
	protected void moveLeft() {
		for (int i = 0; i < cells.length; i++) {
			int oldX = cells[i].getX();
			int newX = oldX - 1;
			cells[i].setX(newX);
		}

	}
	
	/**
	 * ??????????????
	 */
	protected void moveRight() {
		for (int i = 0; i < cells.length; i++) {
			int oldX = cells[i].getX();
			int newX = oldX + 1;
			cells[i].setX(newX);
		}
	}
	
	/**
	 * ????????????????
	 * @return Cell?????
	 */
	protected Cell[] getCells() {
		return cells;
	}
	
	/**
	 * ????????
	 * @return ?????
	 */
	protected Cell getAxis() {
		return axis;
	}
	
	/**
	 * ?????????????????????
	 * @return ??????????
	 */
	protected Cell[] getRotateCells() {
		return rotateCells;
	}
	
	/**
	 * ??cells[0]??????????
	 */
	protected void setAxis() {
		axis = cells[0];
	}
	
	/**
	 * ????????3?????????cells[1]??cells[2]??cells[3]????rotateCells??
	 */
	protected void setRotateCells() {
		rotateCells = new Cell[]{cells[1], cells[2], cells[3]};
	}
}
